﻿using DMSkin.Core.MVVM;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace AnalysisExcel.ViewModel
{
    public class PageAnalysisViewModel : ViewModelBase
    {
        private string selectedFileName = string.Empty;

        public string SelectedFileName
        {
            get { return selectedFileName; }
            set
            {
                selectedFileName = value;
                OnPropertyChanged("SelectedFileName");
            }
        }

        private DataTable excelDataTable1 = null;
        public DataTable ExcelDataTable1
        {
            get { return excelDataTable1; }
            set
            {
                excelDataTable1 = value;
                OnPropertyChanged("ExcelDataTable1");
            }
        }

        private Dictionary<string, string> columsList = null;
        public Dictionary<string,string> ColumsList
        {
            get { return columsList; }
            set
            {
                columsList = value;
                OnPropertyChanged("ColumsList");
            }
        }

        private string selectedFileName2 = string.Empty;

        public string SelectedFileName2
        {
            get { return selectedFileName2; }
            set
            {
                selectedFileName2 = value;
                OnPropertyChanged("SelectedFileName2");
            }
        }

        public ICommand AddFileCommand
        {
            get
            {
                return new DelegateCommand(obj =>
                {
                    System.Windows.Forms.OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog();
                    openFileDialog.Filter = "Excel|*.xls*|所有文件|*.*";
                    if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        //openFileDialog.SelectedPath;
                        //查找文件夹中的数据

                        Task.Run(() =>
                        {
                            if (obj.ToString() == "1")
                            {
                                if (ColumsList != null && ColumsList.Count > 0) columsList.Clear();
                                SelectedFileName = openFileDialog.FileName;
                                DataSet ds = new Helpers.ExcelHelper(SelectedFileName).ExcelToDataSet(true);
                                ExcelDataTable1 = ds?.Tables[0];
                                if (ExcelDataTable1 != null)
                                {
                                    foreach(var col in ExcelDataTable1.Columns)
                                    {
                                        if (ColumsList == null) ColumsList = new Dictionary<string, string>();
                                        ColumsList.Add(col.ToString(), col.ToString());
                                    }
                                }
                            }
                            else
                            {
                                SelectedFileName2 = openFileDialog.FileName;
                            }
                            
                        });
                    }
                });
            }
        }
    }
}
